import numpy as np
import math
import Entities.Missile_AGM as agm
import json,math,os

class UCAV():
    def __init__(self,name,lable,a=0,t=0.,p=0.):
        para = json.load(open('Config/EntitiesConfig.json', 'r', encoding='UTF-8'))
        self.Name = name
        self.Lable = lable
        self.Speed=para['UAV_Speed']
        self.Accelerate=a
        self.Position=[]
        self.Theta=t    # 俯仰
        self.Phi=p  # 偏转
        self.Pointing=[self.Theta,self.Phi]
        self.life=1 # 生存标记
        self.N_M=para['UAV_N_Missiles'] # 剩余弹药数量
        self.Missile_list=[agm.AGM(str(i),self.Name,self.Lable) for i in range(self.N_M)]  # 剩余载弹
        self.P_destroyed=0. # 被摧毁概率 是个过程量

    def Fly_angle(self,t,p,dt):
        '''
        按角度飞
        :param t: 俯仰Theta
        :param p: 偏转Phi
        :param dt: 时间片长度，单位s
        :return: 无
        '''
        assert t >= -math.pi / 18 and t <= math.pi / 18, '输入角度Theta超出范围' + str(t)
        assert p >= -math.pi / 18 and p <= math.pi / 18, '输入角度Phi超出范围' + str(p)
        self.Theta+=t
        self.Phi+=p
        dx = self.Speed * math.sin(self.Theta) * math.cos(self.Phi) * dt
        dy = self.Speed * math.sin(self.Theta) * math.sin(self.Phi) * dt
        dz = self.Speed * math.cos(self.Theta) * dt
        # 判断该空中单位是否已被击毁，若被击毁则不进行坐标更新，若未被击毁则进行坐标更新
        if self.life == 1:
            self.Position[0] += dx
            self.Position[1] += dy
            self.Position[2] += dz
        if self.Z < 0: self.life = 0

    def Fly_grid(self,offset_x,offset_y,offset_z):
        '''
        网格飞行
        :param offset_x: x偏移量
        :param offset_y: y偏移量
        :param offset_z: z偏移量
        :return: 无
        '''
        self.Position[0]+=offset_x
        self.Position[1]+=offset_y
        self.Z=offset_z

    def Fly_model(self,N_model):
        '''
        按动作库飞
        :param N_model: 第N号动作
        :return: 无
        '''
        if N_model==1:
            pass

    def Attack(self,target,pos_opponent,round):
        if len(self.Missile_list) > 0:
            missile=self.Missile_list.pop()
            missile.Launch(target,self.Position,pos_opponent,round)
            return missile
        else: return None






